package com.threeonemeeting.task;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.snda.lib.http.HttpUtil;
import com.snda.lib.http.ICallBack;
import com.threeonemeeting.Const;
import com.threeonemeeting.list.MeetingCommentInfoList;
import com.threeonemeeting.model.MeetingCommentInfo;

// 获取会议的评论
public class GetMeetingCommentsListTask extends ApplicationTask {

	public GetMeetingCommentsListTask(Context context, ICallBack callback) {
		super(context, callback);

		this.bCached = true;
		this.nTaskType = HttpConst.TASKTYPE_MEETINGCOMMENTS;
		this.strUrl = "http://new.31huiyi.com/json/GetEventTopicListHandler.ashx";
	}

	// 具体的task实现该方法，对响应报文进行解析
	protected Map<String, Object> parseResponse(String strResponse) {

		Map<String, Object> mapData = new HashMap<String, Object>();
		mapData.put(HttpConst.KEY_TASK_TYPE, nTaskType);
		mapData.put(HttpUtil.KEY_ERROR_CODE, HttpUtil.ERROR_SUCCESS);
		mapData.put(Const.Keys.KEY_PAGE, nCurrentPages);

		JSONObject jsonResp = null;
		try {
			jsonResp = new JSONObject(strResponse);
		} catch (Exception e) {
			setErr(HttpUtil.ERROR_SERVER_PACKET, mapData);
			return mapData;
		}

		try {
			// 获取 "Setting"部分的内容
			String strSuccess = jsonResp.getString("success");
			if (strSuccess == null) {
				setErr(HttpUtil.ERROR_SERVER_PACKET, mapData);
				return mapData;
			}

			if (strSuccess.equalsIgnoreCase("true") == false) {
				setErr(HttpUtil.ERROR_SERVER_PACKET, mapData);
				return mapData;
			}

			// 附件字段，保存引用的文件
			Map<Integer, String> mapFiles = new HashMap<Integer, String>();

			// files字段
			JSONArray filesJson = jsonResp.getJSONArray("files");
			if (filesJson != null) {

				for (int i = 0, n = filesJson.length(); i < n; i++) {
					JSONObject jData = filesJson.getJSONObject(i);

					try {

						int nTopicId = jData.getInt("TopicId");
						String strImagePath = "http://www.31huiyi.com/"
								+ jData.getString("FullPath");
						mapFiles.put(nTopicId, strImagePath);

					} catch (JSONException ex) {
						Log.e(Const.Tag, ex.getMessage());
					}
				}
			}

			// root根结点，被引用的对象
			MeetingCommentInfoList rootList = new MeetingCommentInfoList();
			JSONArray rootsJson = jsonResp.getJSONArray("roots");
			if (rootsJson != null) {

				for (int i = 0, n = rootsJson.length(); i < n; i++) {
					JSONObject jData = rootsJson.getJSONObject(i);

					MeetingCommentInfo commentInfo = parseMeetingCommentInfo(
							mapFiles, jData);
					if (commentInfo != null) {
						rootList.addCommentInfo(commentInfo);
					}

				}
			}

			// data字段
			MeetingCommentInfoList dataList = new MeetingCommentInfoList();
			JSONArray dataJson = jsonResp.getJSONArray("data");
			if (dataJson != null) {

				for (int i = 0, n = dataJson.length(); i < n; i++) {
					JSONObject jData = dataJson.getJSONObject(i);

					MeetingCommentInfo commentInfo = parseMeetingCommentInfo(
							mapFiles, jData);
					if (commentInfo != null) {
						dataList.addCommentInfo(commentInfo);
					}
				}
			}

			mapData.put(Const.Keys.KEY_LIST, dataList);
			mapData.put("root", rootList);
			setErr(HttpUtil.ERROR_SUCCESS, mapData);
			return mapData;

		} catch (JSONException ex) {
			setErr(HttpUtil.ERROR_SERVER_PACKET, mapData);
			return mapData;
		}
	}

	private MeetingCommentInfo parseMeetingCommentInfo(
			Map<Integer, String> mapFiles, JSONObject jData) {
		try {

			MeetingCommentInfo commentInfo = new MeetingCommentInfo();
			commentInfo.nMeetingId = jData.getInt("EventId");
			commentInfo.nCommentId = jData.getInt("TopicId");
			commentInfo.nParentId = jData.getInt("ParentId");
			commentInfo.nRootId = jData.getInt("RootId");
			commentInfo.nStatus = jData.getInt("Status");
			commentInfo.nReplyNum = jData.getInt("ReplyCount");
			commentInfo.bIsHaveImage = jData.getBoolean("HasFile");
			commentInfo.bIsSuggest = jData.getBoolean("IsSuggest");
			commentInfo.strCommentTime = jData.getString("AddTime");
			commentInfo.nCommentUserId = jData.getInt("UserId");
			commentInfo.strCommentName = jData.getString("UserName");
			commentInfo.strCommentIp = jData.getString("UserIP");
			commentInfo.strCommentText = jData.getString("Content");
			commentInfo.bReplied = (commentInfo.nParentId == 0 ? false : true);

			String strImagePath = mapFiles.get(commentInfo.nCommentId);
			if (strImagePath != null) {
				commentInfo.strImageUrl = strImagePath;
			}

			return commentInfo;
		} catch (JSONException ex) {
			Log.e(Const.Tag, ex.getMessage());
		}

		return null;
	}
}
